<?php


namespace frontend\controllers\cyf;


use frontend\controllers\common;
use logic\cyf\SeckillLogic;
use servers\cyf\EsServer;
use Yansongda\Pay\Pay;
use yii\web\Controller;
use yii\web\Request;

class SeckillController extends Controller
{
    /**
     * 秒杀商品列表
     * @param Request $request
     * @return array|false|string
     * @cyf
     */
    public function actionList(Request $request)
    {
        if(!$request->get()) return common::fail('10001','null','请求方式错误');
        try {
            $lsit = SeckillLogic::list();
            if(!$lsit) throw new \Exception('操作失败');
            return  common::success(20000,$lsit,'操作成功');
        }catch (\Exception $exception){
            return common::fail('30004','null',$exception->getMessage());
        }

    }
    /**
     * 添加秒杀商品
     * @param Request $request
     * @return false|string
     * @cyf
     */
    public function actionCreateKillGoods(Request $request)
    {
        if(!$request->isPost) return common::fail('10001','null','请求方式错误');
        try {
            $seckliiGoods = \Yii::$app->request->queryParams;

            //表单验证
            $res = SeckillLogic::create($seckliiGoods);//添加秒杀商品
            return common::success('20000','null','添加秒杀成功');
        }catch (\Exception $exception){
            return common::fail('30001','null',$exception->getMessage());
        }

    }

    /**
     * 秒杀商品详情
     * @param Request $request
     * @return false|string
     * @cyf
     */
    public function actionDetail(Request $request)
    {
        try {
            if(!$request->isGet) return common::fail('20005','null','请求方式错误');
            $goods_id = $request->get('goods_id');
            if(!is_numeric($goods_id)|| empty($goods_id)) return common::fail(20006,'null','参数错误');
            $goodsDetail = SeckillLogic::detail($goods_id);//调用逻辑
            if(!$goodsDetail) throw new \Exception('操作失败');
            return common::success(0,$goodsDetail,'操作成功');
        }catch (\Exception $exception){
            return common::fail(20006,'null',$exception->getMessage());
        }
    }

    /**
     * 秒杀商品
     * @param Request $request
     * @return array|false|string
     */
    public function actionKillGoods(Request $request)
    {
        if(!$request->isPost) return common::fail('10001','null','请求方式错误');
        try {
            $params = $request->post();
            if(empty($params['id']) || !is_numeric($params['id'])) return common::fail('10002','null','参数错误');
            if(empty($params['user_id']) || !is_numeric($params['user_id'])) return common::fail('10002','null','参数错误');
            $res = SeckillLogic::killGoods($params);
            return  common::success('0',$res,'秒杀成功');
        }catch (\Exception $exception){
            return  common::fail('30002','null',$exception->getMessage());
        }
    }

    /**
     * 发起支付
     * @param Request $request
     * @return array|false|string
     * @cyf
     */
    public function actionPay(Request $request)
    {
        if(!$request->isPost) return common::fail('10001','null','请求方式错误');
        try {
            $order_id = $request->post('oeder_id');
            if(empty($order_id) || !is_numeric($order_id)) return common::fail('10002','null','参数错误');
            $res = \fack\Pay::payType('weixpay');
            print_r($res);die();

        }catch (\Exception $exception){
            return common::fail('40001','null',$exception->getMessage());
        }
    }

    public function actionValiay(Request $request)
    {
        if(!$request->isPost) return common::fail('10001','null','请求方式错误');
        try {

        }catch (\Exception $exception) {

        } finally{

    }
    }

}